<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>添加链接</title>
<meta name="renderer" content="webkit" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1" />
<link href="http://layui.hcwl520.com.cn/layui-v2.4.5/css/layui.css?v=201811010202" rel="stylesheet"></link>
</head>
<body>
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  		<legend>添加链接</legend>
	</fieldset>
	<form class="layui-form"  >
				
				<div class="layui-form-item" >
					<label class="layui-form-label">是否导入微信链接</label>
					<input type="checkbox"  lay-filter="switchImport"  lay-skin="switch" lay-text="是|否">
					
				</div>	
				<div class="layui-form-item" style="display: none" id="importDiv">
					<label class="layui-form-label">微信链接</label>
					<div class="layui-input-block">
						<input type="text"  placeholder="请输入微信链接"
							autocomplete="off" class="layui-input" id="weixinUrl" /> 
							<br/>
						  <button type="button"  class="layui-btn layui-btn" onclick="parseUrl()">解析</button>
					</div>
				</div>
				
				
				<div class="layui-form-item" >
					<label class="layui-form-label">标题</label>
					<div class="layui-input-block">
						<input type="text"  placeholder="请输入标题"
							autocomplete="off" class="layui-input" lay-verify="required" id="title" name="title" />
					</div>
				</div>
				<div class="layui-form-item" >
					<label class="layui-form-label">简介</label>
					<div class="layui-input-block">
						<input type="text"  placeholder="分享链接时显示的简介"
							autocomplete="off" class="layui-input" lay-verify="required" id="describe" name="describe"  />
					</div>
				</div>
				<div class="layui-form-item" >
					<label class="layui-form-label">图标</label>
					<button type="button" class="layui-btn" id="iconPicButton">
  						<i class="layui-icon">&#xe67c;</i>上传图片
					</button> 
					<img  height="45" width="60" id="iconPic"  style="display: none"/>
					<input type="hidden" name="imgUrl"   id="imgUrl" />
				</div>
				<div class="layui-form-item" >
					<label class="layui-form-label">公众号名称</label>
					<div class="layui-input-block">
						<input type="text"  placeholder="请输入公众号名称"
							autocomplete="off" class="layui-input" lay-verify="required" id="author" name="author"  />
					</div>
				</div>
				<div class="layui-form-item" >
					<label class="layui-form-label">显示阅读数</label>
					 <div class="layui-input-block">
						 <input type="checkbox"  checked="" lay-filter="switchRead"  lay-skin="switch" lay-text="显示|隐藏">
						 <input type="hidden"  name="openRead" id="openRead" value="1"/>
					</div>
				</div>
				<div class="layui-form-item" id="readDiv">
					<label class="layui-form-label">阅读数</label>
					<div class="layui-input-inline">
					<input type="text"  placeholder="请输入阅读数"
							autocomplete="off" class="layui-input" id="readNum"  name="readNum" value="0" lay-verify="number"/>
					</div>
					<label class="layui-form-label">好看数</label>
					<div class="layui-input-inline">
					<input type="text"  placeholder="请输入好看数"
							autocomplete="off" class="layui-input" id="likeNum"  name="likeNum" value="0"  lay-verify="number"/>
					</div>
				</div>
				</div>
				<div class="layui-form-item" >
					<label class="layui-form-label">显示留言</label>
					 <div class="layui-input-block">
						 <input type="checkbox" checked="" lay-filter="switchComment"  lay-skin="switch" lay-text="显示|隐藏">
						 <input type="hidden"  name="openComment" id="openComment" value="1"/>
					</div>
				</div>
				<div class="layui-form-item" >
					<label class="layui-form-label">内容</label>
					 <div class="layui-input-block">
						<script id="container" name="content" type="text/plain" >
    					</script>
					</div>
				</div>
			    <div class="layui-input-block" style="display: none">
			      <button class="layui-btn" lay-submit="" lay-filter="add" id="articleSubmint" >立即提交</button>
			      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
   				 </div>
			</form>		
</body>
<script type="text/javascript" src="http://layui.hcwl520.com.cn/jquery/jquery-3.3.1.min.js?v=201804251754"></script>
<script type="text/javascript" src="http://layui.hcwl520.com.cn/layui-v2.4.5/layui.all.js?v=201811010202"></script>
<script type="text/javascript" src="/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="/ueditor/ueditor.all.min.js"></script>  
<script>
layui.use('form', function(){
	  var form = layui.form; 
	  form.render();
	  form.on('switch(switchRead)', function(data){
	  	if(!this.checked){
	  		$("#readDiv").hide();
	  		$("#openRead").val("0");
	  	}else{
	  		$("#readDiv").show();
	  		$("#openRead").val("1");
	  	}
	  });  
	  form.on('switch(switchComment)', function(data){
		  if(!this.checked){
		  		$("#openComment").val("0");
		  	}else{
		  		$("#openComment").val("1");
		  	}
	  }); 
	  form.on('switch(switchImport)', function(data){
		  if(!this.checked){
			  $("#importDiv").hide();
		  	}else{
		  	  $("#importDiv").show();
		  	}
	  }); 
	  form.on('submit(add)', function(data){
    	if(data.field.content==''){
    		layer.msg("请填入正文")
    		return false;
    	}
	    if(data.field.imgUrl==''){
	    	layer.msg("请上传图标");
	    	return false;
	    } 
	    $.ajax({
            //几个参数需要注意一下
                type: "POST",//方法类型
                url: "addArticle" ,//url
                data: JSON.stringify(data.field),
                contentType: 'application/json',
                success: function (result) {
                    if (result == "success") {
						parent.layer.msg('添加成功', {
							icon : 6,
						});
						parent.layer.closeAll('iframe'); 
                    }else{
                    	parent.layer.msg('出现网络故障', {
							icon : 5,
						});
						parent.layer.closeAll('iframe'); 
                    }
                },
                error : function() {
                    layer.alert("异常");
                }
            });
	      return false;
	  });
	});
	
var ue = UE.getEditor('container', {
		initialFrameWidth : '98%',//设置编辑器宽度
		initialFrameHeight : '400',//设置编辑器高度
		scaleEnabled : true
	});
	UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
	UE.Editor.prototype.getActionUrl = function(action) {
		if (action == 'uploadimage' || action == 'uploadscrawl'
				|| action == 'uploadimage') {
			//在这里返回我们实际的上传图片地址
			return "../ueditor/imgUpdate"; //在这里返回我们实际的上传图片地址
		}else if(action == 'catchimage'){
		 console.log(111);	
		} else {
			return this._bkGetActionUrl.call(this, action);
		}
	}
	ue.ready(function() {
		//设置编辑器的内容
		ue.setContent('');
		//获取html内容，返回: <p>hello</p>
		var html = ue.getContent();
		//获取纯文本内容，返回: hello
		var txt = ue.getContentTxt();
	});
layui.use('upload', function(){
  var $ = layui.jquery
  ,upload = layui.upload;
  upload.render({
	    elem: '#iconPicButton'
	    ,url: '/ueditor/imgUpdate'
	    ,accept:'images'
	    ,size: 5120 //限制文件大小，单位 KB
	    ,done: function(res){
	      if(res.state=='SUCCESS'){
	    	  $("#iconPic").show();
	    	  $("#iconPic").attr('src',res.url);
	    	  $("#imgUrl").val(res.url);
	    	  layer.msg("上传成功");
	      }else{
	    	  layer.msg("上传失败");
	      }
	    }
	  });
});	
function parseUrl() {
	var index = layer.load(0, {shade: false});
	var url=$("#weixinUrl").val();
	 $.ajax({
		 url: "parseUrl",
		 data: {'weixinUrl':url},
		 method: 'POST',
		 async:false,
		 success: function(data){
	     	$("#title").val(data.title);
	     	$("#describe").val(data.describe);
	     	$("#iconPic").show();
	     	$("#iconPic").attr('src',data.imgUrl);
	    	$("#imgUrl").val(data.imgUrl);
	     	$("#author").val(data.author);
	     	ue.setContent(formatContent(data.content));
	     	layer.close(index);
	      },
	 	error:function(data){
	 		layer.alert("导入失败");
	 		layer.close(index);
	 	}});
}
function formatContent(source){
		 var rep = /\n+/g;
	    var repone = /<!--.*?-->/ig;
	    var reptwo = /\/\*.*?\*\//ig;
	    var reptree = /[ ]+</ig;
	    var sourceZero = source.replace(rep,"");
	    var sourceOne = sourceZero.replace(repone,"");
	    var sourceTwo = sourceOne.replace(reptwo,"");
	    var sourceTree = sourceTwo.replace(reptree,"<");
	    return sourceTree;
}
</script>
</html>

